﻿using System;
using System.Collections.Generic;
using System.Text;



namespace ForumServer.Forum
{
    [Serializable]
    public class Thread : MarshalByRefObject
    {
        private string        _header;
        private string        _content;
        private int           _numOfReplies;
        private int           _numOfViews;
        private User          _creator;
        private DateTime      _publishDate;
        private DateTime      _lastDate;
        private List<Post>    _postList;
        

        public Thread(string header,string content, User creatorName)
        {
            _header = header;
            _content = content;
            _numOfReplies = 0;
            _numOfViews = 0;
            _creator = creatorName;
            _publishDate = DateTime.Now;
            _lastDate = DateTime.Now;
            _postList = new List<Post>();
        }
        public string Header { get { return _header; }  }
        public int NumOfReplies { get { return _numOfReplies; } set { _numOfReplies = value; } }
        public int NumOfViews { get { return _numOfViews; } set { _numOfViews = value; } }
        public DateTime PublishDate { get { return _publishDate; } }
        public DateTime LastDate { get { return _lastDate; } set { _lastDate = value; } }
        public List<Post> PostList { get { return _postList; } }
        public User Creator { get { return _creator; } }
        public string Content { get { return _content; } set { _content = value; } }


        /// <summary>
        /// add new post to the thread list
        /// </summary>
        /// <param name="p"> the new post </param>
        public void addNewPost(Post p)
        {
            _postList.Add(p);
            _lastDate = DateTime.Now;
        }
        /// <summary>
        /// remove existing post at index i;
        /// </summary>

        public bool removePostAt(int index)
        {
            if (index >= 0 && index < this._postList.Count)
            {
                _postList.RemoveAt(index);
                return true;
            }
            return false;
        }
        /// <summary>
        /// replace the post at the index with p and return true in case the index is valid and false otherwise
        /// </summary>
        /// <param name="index"></param>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool editPostAt(int index, Post p)
        {
            if (index >= 0 && index < this._postList.Count)
            {
                _postList[index] = p;
                return true;
            }
            return false;
        }

        public override String ToString()
        {
            String thread = "";
            thread += "------------------------------------------------\n";
            thread += _header + "\n\n";
            thread += "by " + Creator.Username + " >> " + PublishDate + "\n";
            thread += "Replies: " + NumOfReplies + "\n";
            thread += "Views: " + NumOfViews + "\n";
            if(PostList.Count != 0)
                thread += "Last Post by " + PostList[PostList.Count-1].User.Username + " >> " + LastDate + "\n\n";
            else
                thread += "Last Post by " + Creator.Username + " >> " + LastDate + "\n\n";
            thread += Content + "\n";
            int i = 1;
            foreach (Post p in PostList)
            {
                thread += "\n------------------------------------------------\n";
                thread += "post number " + i + "\n" +  p.ToString();
                i++;
            }

            thread += "------------------------------------------------\n";
            return thread;
        }


    }
}
